package com.zrf.testchatgpt.util;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
@Slf4j
@Component
@Data
@ConfigurationProperties(prefix = "chatgpt")
public class ChatModel {

    private String phonenum;
    private String parentMessageId;

    public  String getgpt(String prompt) throws IOException {
        String url = "https://chatmindai.aabiji.com/api4/chat-process";
        AsyncRequestBuilder asyncRequestBuilder=AsyncRequestBuilder.post(url);
        HttpClient client = HttpClients.createDefault();
        // 外部接口地址
        HttpPost post = new HttpPost(url);
        JSONObject response = null;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("phonenum", phonenum);
        jsonObject.put("prompt", prompt);
        HashMap map = new HashMap();
        map.put("parentMessageId", parentMessageId);
        jsonObject.put("options", map);
        jsonObject.put("template", 1);
        jsonObject.put("viplevel", "0");
        ByteArrayInputStream swapInputStream = null;

        InputStreamReader input = null;
        BufferedReader bf = null;
        try{
            StringEntity stringEntity = new StringEntity((JSONObject.toJSONString(jsonObject)), "application/json", "utf-8");
            post.setEntity(stringEntity);
            post.addHeader("Content-Type", "application/json");
            post.addHeader("Authorization", "Bearer "+phonenum);

            HttpResponse res = client.execute(post);// 上报数据
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                res.getEntity().writeTo(bos);
                 swapInputStream = new ByteArrayInputStream(bos.toByteArray());

                 input = new InputStreamReader(swapInputStream);
                 bf = new BufferedReader(input);

                String line = null;
                StringBuilder sb = new StringBuilder();
                while((line=bf.readLine()) != null){
                    sb.append(line);
                }
                String result1=sb.toString().substring(sb.toString().lastIndexOf("text")+7,sb.toString().lastIndexOf("delta"));
                String result=result1.substring(0,result1.lastIndexOf("delta")-3);
                return  result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (bf!=null){
                bf.close();
            }
            if (input!=null){
                input.close();
            }
            if (swapInputStream!=null){
                swapInputStream.close();
            }

        }
        return "服务器错误";
    }
}
